/* 2017/05/06 */
package org.sample.springboot.aop;

import org.sample.springboot.App;
import org.sample.springboot.converter.print.PrintConverter;
import org.sample.springboot.converter.uppercase.UpperCaseConverter;
import org.sample.springboot.web.AccountRequest;
import org.sample.springboot.web.AccountResponse;
import org.springframework.stereotype.Component;

@Component
public class ConvertManager {

	private static final String ROOT_PACKAGE_NAME = App.class.getPackage().getName();

	private PrintConverter printConverter;

	private UpperCaseConverter upperCaseConverter;

	public ConvertManager() {
		initPrintConverter();
		initUpperCaseConverter();
	}
	
	private void initPrintConverter() {
		if (printConverter == null) {
			printConverter = new PrintConverter();
			printConverter.setChecker(type -> type.getTypeName().startsWith(ROOT_PACKAGE_NAME));
			printConverter.scan(AccountRequest.class, AccountResponse.class);
		}
	}

	private void initUpperCaseConverter() {
		if (upperCaseConverter == null) {
			upperCaseConverter = new UpperCaseConverter();
			upperCaseConverter.setChecker(type -> type.getTypeName().startsWith(ROOT_PACKAGE_NAME));
			upperCaseConverter.scan(AccountResponse.class);
		}
	}

	public void convert(Object object) {
		upperCaseConverter.convert(object);
		printConverter.convert(object);
	}

}
